package com.smartlib.cmnObject.share;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.smartlib.cmnObject.R;

import java.util.ArrayList;

public class ImageTextUDGridAdapter extends BaseAdapter {
    private Context mContext = null;
    private Handler mHandler = null;

    private int mImageWidth = 0;
    private int mImageHeight = 0;
    private int mTextSize = 0;
    private int mTextColor = 0;


    private ArrayList<ImageTextListItem> mArrayList = new ArrayList<>();

    public ImageTextUDGridAdapter() {
    }

    public ImageTextUDGridAdapter(Context context) {
        mContext = context;
    }

    public ImageTextUDGridAdapter(Context context, Handler handler) {
        mContext = context;
        mHandler = handler;
    }

    public void updateContext(Context context) {
        mContext = context;
    }

    public void updateHandler(Handler handler) {
        mHandler = handler;
    }

    public void updateStyle(int textSize, int textColor, int imageWidth, int imageHeight) {
        mTextSize = textSize;
        mTextColor = textColor;
        mImageWidth = imageWidth;
        mImageHeight = imageHeight;
    }


    public void addItem(ImageTextListItem object) {
        mArrayList.add((ImageTextListItem) object);
    }

    public void addItemArrayList(ArrayList<ImageTextListItem> arrayList) {
        for (int i = 0; i < arrayList.size(); i++) {
            mArrayList.add((ImageTextListItem) arrayList.get(i));
        }
    }

    public void removeAll() {
        mArrayList.clear();
    }

    @Override
    public int getCount() {
        return mArrayList.size();
    }

    @Override
    public Object getItem(int arg0) {
        return mArrayList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final ImageTextListItem listItem = mArrayList.get(i);
        ViewHolder viewHolder = null;
        if (null == view) {
            viewHolder = new ViewHolder();

            view = LayoutInflater.from(mContext).inflate(R.layout.gridadapter_imagetext_ud, null);
            TextView textView = (TextView) view.findViewById(R.id.gridadapter_imagetext_ud_textview);
            ImageView imageView = (ImageView) view.findViewById(R.id.gridadapter_imagetext_ud_imageview);
            viewHolder.name = textView;
            viewHolder.image = imageView;

            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }




        if (null != listItem) {
            if (viewHolder.name != null)
                viewHolder.name.setText(listItem.getName());

            if (viewHolder.image != null)
                viewHolder.image.setImageResource(listItem.getImageResId());

        }

        return view;
    }

    private void updateList() {

    }


    private static class ViewHolder {
        TextView name;
        ImageView image;
    }


}
